package org.example.day09;

/**
 * 第三章节3.2.6: 方法join()后的代码提前运行
 */
public class MyThread7 extends Thread{
    private Thread7B tb;

    public MyThread7(Thread7B tb) {
        this.tb = tb;
    }

    @Override
    public void run() {
        try {
            synchronized (tb){
                System.out.println("A begin: " + Thread.currentThread().getName() + " time = " + System.currentTimeMillis());
                Thread.sleep(500);
                System.out.println("A end: " + Thread.currentThread().getName() + " time = " + System.currentTimeMillis());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            Thread7B tb = new Thread7B();
            MyThread7 ta = new MyThread7(tb);
            // ta.start(); | tb.start(); | tb.join(200); 都有可能先执行.存在多种可能
            ta.start();
            tb.start();
            tb.join(200);
            System.out.println("main end: " + System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Thread7B extends Thread{
    @Override
    synchronized public void run() {
        try {
            System.out.println("B begin: " + Thread.currentThread().getName() + " time = " + System.currentTimeMillis());
            Thread.sleep(500);
            System.out.println("B end: " + Thread.currentThread().getName() + " time = " + System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}